package cn.dingdan.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.dingdan.pojo.Dishes;
import cn.dingdan.pojo.Type;
import cn.dingdan.sercice.DishesService;
import cn.dingdan.sercice.TypeService;
import cn.dingdan.tools.PageSupport;

@Controller
public class PrividerController {
	
	@Resource(name="dishesServiceImpl")
	private DishesService dishesService;
	
	
	@Resource(name="typeServiceImpl")
	private TypeService typeService;
	
	
	@RequestMapping(value="privider",method=RequestMethod.GET)
	public String loginBoss23(HttpSession session,Model model,
			@RequestParam(value="currentPageNo",defaultValue="1",required=false)Integer currentPageNo,
			@RequestParam(value="menuTypeId",required=false)Integer menuTypeId,
			@RequestParam(value="menuId",required=false)Integer menuId,
			@RequestParam(value="dishPicture",required=false)String dishPicture){
		int num=dishesService.delDishes(menuId);
		String path=session.getServletContext().getRealPath("/statics/imagesTow/"+dishPicture);
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		PageSupport ps=new PageSupport();
		ps.setPageSize(5);
		ps.setTotalCount(dishesService.getDishesSize(menuTypeId));
		ps.setCurrentPageNo(currentPageNo);
		List<Dishes> lists = dishesService.getLists(menuTypeId, ps);
		ps.setList(lists);
		List<Type> types = typeService.getLists();
		model.addAttribute("types", types);
		session.setAttribute("lists", ps);
		model.addAttribute("num", num);
		return "privider";
	}
		
	@RequestMapping(value="privider",method=RequestMethod.POST)
	public String addPrivider(
			@RequestParam(value="dish",required=false)String dish,
			@RequestParam(value="dishDetails",required=false)String dishDetails,
			@RequestParam(value="pic",required=false)MultipartFile pic,
			@RequestParam(value="dishPrice",required=false)Double dishPrice,
			@RequestParam(value="menuTypeId",required=false)Integer menuTypeId,
			HttpSession session,Model model
			) throws  IOException{
		Dishes dishes = new  Dishes();
		dishes.setDish(dish);
		dishes.setDishDetails(dishDetails);
		dishes.setDishPrice(dishPrice);
		dishes.setLike(0);
		List<Type> types =typeService.getLists();
		for (Type type : types) {
			if (type.getMenuTypeId()==menuTypeId) {
				dishes.setMenuTypeId(menuTypeId);
			}
		}
		if (!pic.isEmpty()) {
			
			String beginIndex=pic.getOriginalFilename();
			String originalFilename =pic.getOriginalFilename().substring(beginIndex.lastIndexOf("."));
			String path=session.getServletContext().getRealPath("/statics/imagesTow/");
			String fileName=UUID.randomUUID()+"_"+System.currentTimeMillis()+originalFilename;
			File file = new File(path, fileName);
			pic.transferTo(file);
			dishes.setDishPicture(fileName);
		}
		int num=dishesService.addDishes(dishes);
		model.addAttribute("num", num);
		return "redirect:privider";
	}
	
	
	@RequestMapping(value="update",method=RequestMethod.POST)
	public String updatePrivider(
			@RequestParam(value="dish",required=false)String dish,
			@RequestParam(value="dishDetails",required=false)String dishDetails,
			@RequestParam(value="pic",required=false)MultipartFile pic,
			@RequestParam(value="dishPrice",required=false)Double dishPrice,
			@RequestParam(value="menuTypeId",required=false)Integer menuTypeId,
			@RequestParam(value="like",required=false)Integer like,
			@RequestParam(value="menuId",required=false)Integer menuId,
			@RequestParam(value="dishPicture",required=false)String dishPicture,
			HttpSession session,Model model
			) throws  IOException{
		Dishes dishes = new  Dishes();
		dishes.setMenuId(menuId);
		dishes.setDish(dish);
		dishes.setDishDetails(dishDetails);
		dishes.setDishPrice(dishPrice);
		dishes.setLike(like);
		List<Type> types =typeService.getLists();
		for (Type type : types) {
			if (type.getMenuTypeId()==menuTypeId) {
				dishes.setMenuTypeId(menuTypeId);
			}
		}
		String path_01=session.getServletContext().getRealPath("/statics/imagesTow/"+dishPicture);
		File file_01 = new File(path_01);
		if (file_01.exists()) {
			file_01.delete();
		}
		
		
		if (!pic.isEmpty()) {
						
			String beginIndex=pic.getOriginalFilename();
			String path=session.getServletContext().getRealPath("/statics/imagesTow/");
			String originalFilename =pic.getOriginalFilename().substring(beginIndex.lastIndexOf("."));
			String fileName=UUID.randomUUID()+"_"+System.currentTimeMillis()+originalFilename;
			File file = new File(path, fileName);
			pic.transferTo(file);
			dishes.setDishPicture(fileName);
		}
		int num=dishesService.updateDishes(dishes);
		model.addAttribute("num", num);
		return "redirect:privider";
	}
}
